const computedbehavior = require("miniprogram-computed").behavior;
import { reqselectcart } from "@/api/cart";
import { getdefaultAddress } from "../../../api/address";
const app = getApp();
Page({
  behaviors: [computedbehavior],
  data: {
    buyName: "", // 订购人姓名
    buyPhone: "", // 订购人手机号
    deliveryDate: "选择送达日期", // 期望送达日期
    blessing: "", // 祝福语
    show: false, // 期望送达日期弹框
    minDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    goodlist: [],
    address: {},
    iseditback: "",
    isorder: "false",
  },
  computed: {
    total(data) {
      let total = 0;
      data.goodlist.forEach((e) => {
        total += e.price * e.count;
      });
      return total;
    },
  },
  // 选择期望送达日期
  onShowDateTimerPopUp() {
    this.setData({
      show: true,
    });
  },
  // 期望送达日期确定按钮
  onConfirmTimerPicker(event) {
    this.setData({
      show: false,
    });
  },
  // 期望送达日期取消按钮 以及 关闭弹框时触发
  onCancelTimePicker() {
    this.setData({
      show: false,
      minDate: new Date().getTime(),
      currentDate: new Date().getTime(),
    });
  },
  // 添加收货地址
  toAddress() {
    console.log(333);
    wx.navigateTo({
      url:
        "/modules/settingModule/modules/settingmodule/pages/address/list/index",
    });
  },
  async getaddress() {
    if (app.temptaddress.name) {
      console.log(1);
      this.setData({
        address: app.temptaddress,
      });
    } else {
      const res = await getdefaultAddress();
      this.setData({
        address: res.data,
      });
    }
  },
  async onLoad(options) {
      // 获取默认地址
      this.getaddress();
    const { iseditback } = options;
    if (iseditback == "true") {
      if (app.flag == true) {
        this.setData({
          goodlist: app.appgoodlist,
        });
      } else {
        // 先前由购物车跳转到订单页
        const res = await reqselectcart();
        this.setData({
          goodlist: res.data,
        });
      }
      return;
    }
    if (options.flag == "true") {
      // 直接购买
      if (options.flag == "true") {
        this.setData({
          "goodlist[0]": options,
          isorder: "true",
        });
      }
      app.appgoodlist = [options];
    } else {
      // 由购物车跳转到订单页
      const res = await reqselectcart();
      this.setData({
        goodlist: res.data,
      });
    }
  },
  onUnload() {
    app.flag = false;
    app.temptaddress={}
  },
});
